package com.software.graduationproject.modules.comment.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.Page;

import com.software.graduationproject.commons.exception.ServiceException;
import com.software.graduationproject.commons.select.Select;
import com.software.graduationproject.modules.comment.entity.Comment;
import com.software.graduationproject.modules.comment.mapper.CommentMapper;
import com.software.graduationproject.modules.comment.object.CommentVo;
import com.software.graduationproject.modules.comment.object.CommentCreateQo;
import com.software.graduationproject.modules.comment.object.CommentUpdateQo;

import com.software.graduationproject.modules.marketers.service.MarketersService;
import com.software.graduationproject.modules.order.entity.OrderInfo;
import com.software.graduationproject.modules.order.mapper.OrderInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * CommentService : 评论表
 * ---------------------------------------------------------------------------------------------------------------------
 */
@Service
public class CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private MarketersService marketersService;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    public void createQo(CommentCreateQo qo){
        Comment comment = new Comment();
        BeanUtil.copyProperties(qo,comment);
        Long orderId = qo.getOrderId();
        OrderInfo orderInfo = orderInfoMapper.selectById(orderId);
        if (orderInfo.getStatus() == 0L) {
            orderInfo.setStatus(1L);
            orderInfoMapper.updateById(orderInfo);
        }
        commentMapper.insert(comment);
        marketersService.selectMarketersComment(qo.getMarketersId());
    }

    public void updateQo(CommentUpdateQo qo){
        Comment comment = commentMapper.selectById(qo.getId());
        if (comment == null){
            throw  new ServiceException("数据不存在");
        }

        BeanUtil.copyProperties(qo,comment);
        commentMapper.updateById(comment);
    }

    public CommentVo selectVo(Long id) {
        CommentVo commentVo = new CommentVo();
        Comment comment = commentMapper.selectById(id);
        BeanUtil.copyProperties(comment, commentVo);
        return commentVo;
    }

    public void delete(Long id) {
        commentMapper.deleteById(id);
    }

    public Page<CommentVo> selectPageVo(Select select){
        return commentMapper.selectPageVo(select);
    }

    public List<CommentVo> selectListVo(Select select){
        return commentMapper.selectListVo(select);
    }
}
